package com.example.express_user.service;

import com.example.express_user.dao.UserDao;
import com.example.express_user.domain.MyResponse;
import com.example.express_user.domain.User;
import com.example.express_user.domain.UserAuth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserService {
    final private String COMMON_USER_AUTH="普通用户";
    final private String COURIER_USER_AUTH="快递员";
    final private int STATUS_CODE_200=200;
    final private int STATUS_CODE_500=500;
    final private String DEFAULT_FACE="default-face.png"; //默认头像
    @Autowired
    UserDao userDao;

    /**
     * 用户注册
     * @param userPhone
     * @param userPassword
     * @return
     */
    @Transactional
    public MyResponse commonUserRegister(String userPhone, String userPassword){
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String encodePassword = encoder.encode(userPassword);
        //添加用户信息
        Boolean user = userDao.addUser(userPhone, encodePassword, userPhone,DEFAULT_FACE);
        //初始化用户权限
        Boolean addUserAuth = userDao.addUserAuth(userPhone, COMMON_USER_AUTH);
        //初始化用户地址
        Boolean initDeliveryAddress = userDao.initDeliveryAddress(userPhone);
        MyResponse response;
        if (user&&addUserAuth&&initDeliveryAddress){
            response = new MyResponse(STATUS_CODE_200, true);
        }else {
            response = new MyResponse(STATUS_CODE_500, false);
        }
        return response;
    }

    /**
     * 普通用户注册快递员
     * @param realName
     * @param idCard
     * @param familyAddress
     * @param userSex
     * @param userPhone
     * @return
     */
    @Transactional
    public MyResponse commonUserRegisterCourier(String realName, String idCard, String familyAddress, String userSex, String userPhone){
        int user_sex_int=0;
        if (userSex.equals("女")){
            user_sex_int=1;
        }
        Boolean updateUserRealInfo = userDao.updateUserRealInfo(realName, idCard, familyAddress, user_sex_int, userPhone);
        Boolean addUserAuth = userDao.addUserAuth(userPhone, COURIER_USER_AUTH);
        MyResponse response;
        if (updateUserRealInfo&&addUserAuth){
            response = new MyResponse(STATUS_CODE_200, true);
        }else {
            response = new MyResponse(STATUS_CODE_500, false);
        }
        return response;

    }

    /**
     * 修改昵称
     * @param userPhone
     * @param userNetName
     * @return
     */
    public MyResponse updateUserNetName(String userPhone, String userNetName){
        Boolean aBoolean = userDao.updateUserNetName(userPhone, userNetName);
        MyResponse response;
        if (aBoolean){
            response = new MyResponse(STATUS_CODE_200, true);
        }else {
            response = new MyResponse(STATUS_CODE_500, false);
        }
        return response;
    }

    /**
     * 修改常用收货地址
     * @param userPhone
     * @param deliveryAddress
     * @return
     */
    public MyResponse updateDeliveryAddress(String userPhone, String deliveryAddress){
        Boolean aBoolean = userDao.updateDeliveryAddress(userPhone, deliveryAddress);
        MyResponse response;
        if (aBoolean){
            response = new MyResponse(STATUS_CODE_200, true);
        }else {
            response = new MyResponse(STATUS_CODE_500, false);
        }
        return response;
    }

    /**
     * 修改头像
     * @param userPhone
     * @param userFace
     * @return
     */
    public MyResponse updateUserFace(String userPhone, String userFace){
        Boolean aBoolean = userDao.updateUserFace(userPhone, userFace);
        MyResponse response;
        if (aBoolean){
            response = new MyResponse(STATUS_CODE_200, true);
        }else {
            response = new MyResponse(STATUS_CODE_500, false);
        }
        return response;
    }

    /**
     * 查找用户基本信息
     * @param userPhone
     * @return
     */
    public User findUserInfoByPhone(String userPhone){
        User user = userDao.findUserByPhone(userPhone);
        user.setUserDeliveryAddress(userDao.findUserDeliveryAddressByPhone(userPhone));

        return user;
    }

    /**
     * 查找权限
     * @param userPhone
     * @return
     */
    public List<UserAuth> findUserAuthByPhone(String userPhone){
        return userDao.findUserAuthByPhone(userPhone);
    }





}
